﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class LoaiNguoiDungDAO
    {
        public static List<LoaiNguoiDungDTO> getAll()
        {
            List<LoaiNguoiDungDTO> result = new List<LoaiNguoiDungDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LOAINGUOIDUNGs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                LoaiNguoiDungDTO dto = new LoaiNguoiDungDTO();

                dto.MaLoaiNguoiDung = ds.MaLoaiNguoiDung;
                dto.TenLoaiNguoiDung = ds.TenLoaiNguoiDung;
                
                result.Add(dto);
            }

            return result;
        }

        public static LoaiNguoiDungDTO getAlongCode(int maLoai)
        {
            LoaiNguoiDungDTO result = new LoaiNguoiDungDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LOAINGUOIDUNGs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaLoaiNguoiDung == maLoai).Single();

            result.MaLoaiNguoiDung = query.MaLoaiNguoiDung;
            result.TenLoaiNguoiDung = query.TenLoaiNguoiDung;

            return result;
        }

        public static void Insert(LoaiNguoiDungDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LOAINGUOIDUNG loai = new LOAINGUOIDUNG();
            loai.TenLoaiNguoiDung = _insert.TenLoaiNguoiDung;
            loai.Deleted = _insert.Deleted;
            
            dbRaoVat.LOAINGUOIDUNGs.InsertOnSubmit(loai);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maLoai)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LOAINGUOIDUNG _delete = (from n in dbRaoVat.LOAINGUOIDUNGs
                                 where n.MaLoaiNguoiDung == maLoai && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(LoaiNguoiDungDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LOAINGUOIDUNG _update = (from n in dbRaoVat.LOAINGUOIDUNGs
                                     where n.MaLoaiNguoiDung == dto.MaLoaiNguoiDung && n.Deleted == 0
                                     select n).Single();

            _update.TenLoaiNguoiDung = dto.TenLoaiNguoiDung;
            
            dbRaoVat.SubmitChanges();
        }

        public static int getMa(string tenLoaiND)
        {
            if (null == tenLoaiND)
                return 1;

            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LOAINGUOIDUNGs.Where(dm => dm.Deleted.Equals(0) &&
                dm.TenLoaiNguoiDung == tenLoaiND).Single();

            return query.MaLoaiNguoiDung;
        }
    }
}
